import { post, BaseRes } from "../libs/request";
import { AdvicesType, DingdanStat, Relevance } from "../models/enums";
import { IDingdan, IBooks, IUser } from "../models/types";
// API路径前缀
const PREFIX = "/api/dingdan";

export interface Booklist extends BaseRes {
  rows: IBooks[];
}
export interface advlist {
  _id: string;
  seen: boolean;
  time: number;
  tyep: AdvicesType;
  dingdan: IDingdan;
  cart: {
    prices: number[];
    names: string[];
    allprice: number;
    confirm: DingdanStat;
  };
  book: IBooks;
}
export interface Dingdanlist extends BaseRes {
  data: {
    list: IDingdan[];
    total: number;
  };
}
export interface Dingdan extends BaseRes {
  data: IDingdan;
}
export interface Advices extends BaseRes {
  data: {
    list: advlist[];
    total: number;
  };
}

export async function dingdan(cartIds: string[], allprice: number, value: any) {
  const { data } = await post<BaseRes>(PREFIX + "/add", {
    cartIds,
    allprice,
    ...value,
  });
  return data;
}

export async function dingdanlist(userId: string, skip: number, limit: number) {
  const { data } = await post<Dingdanlist>(PREFIX + "/list", {
    userId,
    skip,
    limit,
  });
  return data;
}

export async function booklist(bookIds: string[], token: string) {
  const { data } = await post<Booklist>(
    PREFIX + "/list/book",
    { bookIds },
    token
  );
  return data;
}

export async function datail(dingdanId: string, cartId: string, token: string) {
  const { data } = await post<Dingdan>(
    PREFIX + "/detail",
    { dingdanId, cartId },
    token
  );
  return data;
}

// 确认或者取消订单
export async function confirm(cartId: string, flag: boolean) {
  const { data } = await post<BaseRes>(PREFIX + "/confirm", {
    cartId,
    flag,
  });
  return data;
}

export async function sendlist(skip: number, limit: number) {
  const { data } = await post<Advices>(PREFIX + "/list/send", { skip, limit });
  return data;
}

export async function see(advId: string) {
  const { data } = await post<BaseRes>(PREFIX + "/list/see", { advId });
  return data;
}

export async function deal(advId: string, flag: boolean) {
  const { data } = await post<BaseRes>(PREFIX + "/list/deal", { advId, flag });
  return data;
}
